
DEFINE VARIABLE outto AS CHARACTER  NO-UNDO INITIAL "R2014.csv".

FOR FIRST CODE_mstr NO-LOCK WHERE CODE_fldname = "HKR2014" 
                              AND CODE_value = "outpath":
END.
IF AVAIL CODE_mstr THEN DO:
    ASSIGN outto = code_cmmt + outto .
END.

OUTPUT TO VALUE(outto). 
PUT "ap_vd~tvd_term~tap_ref~tap_date~tap_Duedate~tap_amt~tap_AmtApp~tap_hold"
     skip.
for each vd_mstr FIELDS(vd_addr vd_cr_term) NO-LOCK  :
    for each ap_mstr FIELDS(ap_vend ap_type ap_ref ap_date ap_base_amt)
         where ap_vend = vd_addr AND ap_type = "VO"
         NO-LOCK:

        FOR FIRST vo_mstr FIELDS(vo_ref vo_recur vo_confirmed vo_due_date
                                 vo_base_app vo_hold vo_cr_term)
             where vo_ref       = ap_ref
            and   vo_recur     = no
            and   vo_confirmed = true:
        END.
        IF AVAIL vo_mstr THEN DO:
/*           IF ap_base_amt <> vo_base_app THEN DO:  */
              EXPORT DELIMITER "~t" ap_vend vo_cr_term ap_ref 
                        string(year(ap_date),"9999") + "-" + 
                        string(MONTH(ap_date),"99") + "-" + 
                        string(DAY(ap_date),"99")
                        string(year(vo_due_date),"9999") + "-" + 
                        string(MONTH(vo_due_date),"99") + "-" + 
                        string(DAY(vo_due_date),"99")
                        ap_base_amt vo_base_app vo_hold .
/*           END.  */
        END.
      END.
END.
OUTPUT CLOSE.
QUIT.
